package Filter;

import StudentDTO.UserInfoDTO;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 黄忠
 * Created by 黄忠 on 2019-04-09.
 */
@WebFilter(filterName = "LogFilter")
public class LogFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
        res.setContentType("application/json");
        res.setCharacterEncoding("UTF-8");
        System.out.println(this.getClass());

        HttpSession session = req.getSession(true);
        UserInfoDTO uid=(UserInfoDTO) session.getAttribute("UserInfor");

        if(uid!=null){
            String submit=req.getParameter("submit");
            if(submit!=null&&submit.equals("退出")){
                res.sendRedirect("https://cas.dgut.edu.cn/logout?callback=http://localhost:8080/HomePage.jsp");
                session.invalidate();
                System.out.println("退出");
            }
            else res.sendRedirect("http://localhost:8080/HomePage.jsp");
        }
        else chain.doFilter(req,res);
    }

}
